package com.ctshk.app.system.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.rpc.system.dto.SysMenuActionButtonDTO;
import com.ctshk.rpc.system.dto.SysMenuDTO;
import com.ctshk.rpc.system.req.SysMenuReq;
import com.ctshk.rpc.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-23
 */
@Api(value="菜单", tags="功能管理")
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController {

    @DubboReference
    private ISysMenuService sysMenuService;

    @ApiOperation(value="新增功能菜单", notes="新增功能菜单")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增功能菜单", operDesc = "新增功能菜单")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid SysMenuReq sysMenuReq) {
        sysMenuService.add(sysMenuReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="删除功能菜单", notes="删除功能菜单")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.DELETE, operTitle = "删除功能菜单", operDesc = "删除功能菜单")
    @GetMapping("/del/{id}")
    public Result del(@PathVariable long id) {
        sysMenuService.del(id, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="编辑功能菜单", notes="编辑功能菜单")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑功能菜单", operDesc = "编辑功能菜单")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid SysMenuReq sysMenuReq) {
        sysMenuService.update(sysMenuReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="查询功能菜单", notes="查询功能菜单-此查询返回分组的树形结构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询功能菜单", operDesc = "查询功能菜单")
    @GetMapping("/query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysType", value = "系统类型 1 核心业务系统;2 供应商开放平台;3 代理商分销系统ADS;4 地接系统;5 商旅系统TMC"),
            @ApiImplicitParam(name = "id", value = "菜单id"),
            @ApiImplicitParam(name = "userId", value = "设置对象用户id,不传则查询当前账号")
    })
    public Result<List<SysMenuDTO>> query(@RequestParam(name = "sysType", defaultValue = "1") Integer sysType, @RequestParam(name = "id", defaultValue = "0", required = false) Long id, @RequestParam(name = "userId", defaultValue = "0", required = false) Long userId) {
        return Result.success(sysMenuService.query(sysType, id, userId == 0 ? getCurrentUser(getRequest()).getId() : userId));
    }

    @ApiOperation(value="查询有权限的功能菜单", notes="查询功能菜单-此查询返回所有菜单的树形结构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询功能菜单", operDesc = "查询功能菜单")
    @GetMapping("/query-all")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysType", value = "系统类型 1 核心业务系统;2 供应商开放平台;3 代理商分销系统ADS;4 地接系统;5 商旅系统TMC"),
            @ApiImplicitParam(name = "userId", value = "设置对象用户id,不传则查询当前账号")
    })
    public Result<List<SysMenuDTO>> queryALL(@RequestParam(name = "sysType", defaultValue = "1") Integer sysType, @RequestParam(name = "userId", defaultValue = "0", required = false) Long userId) {
        return Result.success(sysMenuService.queryAll(sysType, userId == 0 ? getCurrentUser(getRequest()).getId() : userId));
    }

    @ApiOperation(value="查询功能按钮", notes="查询功能菜单下所有功能按钮")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询功能菜单", operDesc = "查询功能菜单")
    @GetMapping("/query-button")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单id"),
            @ApiImplicitParam(name = "userId", value = "设置对象用户id或者快速查询用户id,不传则查询当前账号")
    })
    public Result<List<SysMenuActionButtonDTO>> queryButton(@RequestParam(name = "menuId") Long menuId, @RequestParam(name = "userId", defaultValue = "0", required = false) Long userId) {
        return Result.success(sysMenuService.queryActionButton(menuId, userId == 0 ? getCurrentUser(getRequest()).getId() : userId));
    }

    @ApiOperation(value="设置功能菜单是否有效", notes="设置功能菜单是否有效")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "设置功能菜单是否有效", operDesc = "设置功能菜单是否有效")
    @GetMapping("/set-status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单id"),
            @ApiImplicitParam(name = "status", value = "状态0=无效;1有效")
    })
    public Result setStatus(@RequestParam(name = "id") Long id, @RequestParam(name = "status") int status) {
        sysMenuService.setStatus(id, status, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

}
